/**<P>Title:ucweb</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2010</p>
 *<p>Company: ucweb.com</p>
 *@author chensl@ucweb.com
 *@version 
 */
package com.ucweb.pooltest;

import com.ucweb.pooldemo.R;
import com.ucweb.pooltest.common.Constant;
import com.ucweb.pooltest.util.Setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * @author chensl@ucweb.com
 * 
 *         下午7:47:52 2014-8-19
 */
public class SettingActivity extends ActionBarActivity {
	private Fragment mSettingFragment;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);
		mSettingFragment = new SettingFragment();
		getSupportActionBar().setDisplayShowHomeEnabled(true);
		getSupportActionBar().setTitle(
				getString(R.string.menu_item_setting_str));
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_content, mSettingFragment).commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// Respond to the action bar's Up/Home button
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public class SettingFragment extends Fragment implements OnClickListener {
		private TextView mAllocateTimeTextView;

		// TODO Auto-generated constructor stub
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.fragment_setting,container,false);

			initView(view);
			return view;
		}

		private void initView(View view) {
			// TODO Auto-generated method stub
			mAllocateTimeTextView = (TextView) view
					.findViewById(R.id.setting_allocate_id);
			mAllocateTimeTextView.setText(String.valueOf(Setting.getInstance()
					.getAllocateCount()));
			mAllocateTimeTextView.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			int mode = 0;
			switch (v.getId()) {
			case R.id.setting_allocate_id:
				mode = Constant.FLAG_SET_ALLOCATE_COUNT;
				break;
			default:
				break;
			}
			Intent intent = new Intent(getActivity(), SetInfoActivity.class);
			Bundle args = new Bundle();
			args.putInt(Constant.FLAG_MODE, mode);
			intent.putExtras(args);
			startActivityForResult(intent, mode);
		}

		@Override
		public void onActivityResult(int requestCode, int resultCode,
				Intent data) {
			switch (requestCode) {
			case Constant.FLAG_SET_ALLOCATE_COUNT:
				mAllocateTimeTextView.setText(String.valueOf(Setting
						.getInstance().getAllocateCount()));
				break;
			default:
				break;
			}
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}